package com.young.sync.factory;

import com.young.sync.service.entity.TopicEntity;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description：一个 Topic 绑定一个 MessageListener
 * @author: yangyahui01
 * @date: 12/5/24 7:43 PM
 */
public class MessageListenerFactory {
    private static Map<String, TopicEntity> services = new ConcurrentHashMap<String, TopicEntity>();

    /**
     * MessageListener注册
     */
    public static void register(String bizName, TopicEntity conf) {
        Assert.notNull(bizName, "bizName不能为null");
        services.put(bizName, conf);
    }

    /**
     * 获取全部注册的MessageListener
     */
    public static List<TopicEntity> getAllMessageListener() {
        return new ArrayList<TopicEntity>(services.values());
    }

    /**
     * 判断是否存在通过bizName
     */
    public static boolean isValidMessageListener(String bizName) {
        return services.containsKey(bizName);
    }

    /**
     * 获取MessageListener通过bizName
     */
    public static TopicEntity getMessageListenerByTopic(String bizName) {
        return services.get(bizName);
    }
}
